package com.linglingC.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.linglingC.mapper.EmplyeeMapper;
import com.linglingC.pojo.Emplyee;
import com.linglingC.pojo.PageBean;
import com.linglingC.service.EmplyeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class EmplyeeServiceImpl implements EmplyeeService {

    @Autowired
    private EmplyeeMapper emplyeeMapper;


    @Override
    public PageBean findAll(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        List<Emplyee> list=emplyeeMapper.findAll();

        Page<Emplyee> p=(Page<Emplyee>) list;

        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());

        return pageBean;
    }

    @Override
    public PageBean select(String key, String value, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        Emplyee emplyee=setKeyValue(key,value);

        List<Emplyee> list=emplyeeMapper.selectP(emplyee);

        Page<Emplyee> p=(Page<Emplyee>) list;

        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());

        return pageBean;
    }

    @Override
    public void addEmplyee(Emplyee emplyee) {
        emplyeeMapper.addEmplyee(emplyee);
    }

    @Override
    public void deleteOneEmplyee(Integer ID) {
        emplyeeMapper.deleteOneEmplyee(ID);
    }

    @Override
    public void editEmplyee(Emplyee emplyee) {
        emplyeeMapper.editEmplyee(emplyee);
    }


    private Emplyee setKeyValue(String key, String value)
    {
        Emplyee emplyee=new Emplyee();
        switch (key){
            case "ID":
                emplyee.setID(Integer.parseInt(value));
                break;
            case "姓名":
                emplyee.setName(value);
                break;
            case "性别":
                emplyee.setGender(value);
                break;
            case "电话号码":
                emplyee.setTelephone(value);
                break;
            case "身份证号":
                emplyee.setIdcard(value);
                break;
            case "家庭住址":
                emplyee.setAddress(value);
                break;
            default:
                break;
        }
        return emplyee;
    }
}
